package edu.nf.vac.admin.filter;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.text.StringEscapeUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

/**
 * @Author : QYM
 * @create 2022/4/2
 * HttpServletRequestWrapper是一个装饰类，在request到达servlet之前拦截HttpServletRequest对象；
 * 由于java.util.Map包装的HttpServletRequest对象的参数是不可改变的，
 * 所以上文中编写的内部类Request就是HttpServletRequest类的装饰类
 * 该类继承的HttpServletRequestWrapper类是HttpServletRequest类的装饰类。
 *
 * HttpServletRequestWrapper装饰类作用：使请求对象参数可变
 */
@Slf4j
public class XssHttpRequestWrapper extends HttpServletRequestWrapper {
    public XssHttpRequestWrapper(HttpServletRequest request) {
        super(request);
    }

    @Override
    public String getHeader(String name) {
        return StringEscapeUtils.escapeHtml4(super.getHeader(name));
    }

    @Override
    public String getParameter(String name) {
        return StringEscapeUtils.escapeHtml4(super.getParameter(name));
    }

    @Override
    public String[] getParameterValues(String name) {
        String[] values = super.getParameterValues(name);
        if (values != null){
            String[] escapeValues = new String[values.length];
            for (int i = 0; i < values.length; i++) {
                escapeValues[i] = StringEscapeUtils.escapeHtml4(values[i]);
            }
            return escapeValues;
        }
        return super.getParameterValues(name);
    }
}
